package com.appview;

import com.util.Config;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * Created by QiHuang on 2015/10/26.
 * 此类用于管理员增删改查信息
 */
public class InfoOperation extends JFrame implements ActionListener {

    private JLabel[] jLabels;
    private JTextField[] jTextFields;
    private JButton jButton1, jButton2 ,jButton3;
    private JPanel[] jPanels;
    private int type; //1修改或者0添加
    private List<String> info;
    private static InfoOperation infoOperation;
    private TextTip textTip;

    public InfoOperation(int type, List<String> info) {
        this.type = type;
        this.info = info;
        infoOperation = this;
    }

    public InfoOperation(int type) {
        this.type = type;
        infoOperation = this;
        textTip = new TextTip();
    }

    public static InfoOperation getInfoOperation() {
        return infoOperation;
    }

    public void openView() {
        initComponent();

        for (JPanel jPanel: jPanels) {
            add(jPanel);
        }
        setLayout(new GridLayout(jPanels.length, 1));
        setVisible(true);
        setSize(300, 400);
        setLocation(400, 200);
        setResizable(false);
        setTitle("操作中...");
        setAlwaysOnTop(true);
    }

    private void initComponent() {

        if (Config.getType() == 0) {
            jLabels = new JLabel[11];
            jTextFields = new JTextField[11];
            jPanels = new JPanel[12];
            for (int i = 0; i < Config.TEAC_LIST.length; i++) {
                jLabels[i] = new JLabel(Config.TEAC_LIST[i]);
                jTextFields[i] = new JTextField(10);
            }
            for (int i = 0; i < Config.TEAC_LIST.length + 1; i++) {
                jPanels[i] = new JPanel();
            }
        } else if (Config.getType() == 1) {
            jLabels = new JLabel[9];
            jTextFields = new JTextField[9];
            jPanels = new JPanel[10];
            for (int i = 0; i < Config.STU_LIST.length; i++) {
                jLabels[i] = new JLabel(Config.STU_LIST[i]);
                jTextFields[i] = new JTextField(10);
            }
            for (int i = 0; i < Config.STU_LIST.length + 1; i++) {
                jPanels[i] = new JPanel();
            }
        }
        if (type == 0) {
            jButton1 = new JButton("添加");
        } else {
            jButton1 = new JButton("修改");
            jTextFields[0].setEditable(false);
            for (int i = 0; i < jTextFields.length; i++) {
                jTextFields[i].setText(info.get(i));
            }
        }

        jButton2 = new JButton("返回");
        jButton3 = new JButton("文本格式");
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
        jButton3.addActionListener(this);
        for (int i = 0; i < jPanels.length - 1; i++){
            jPanels[i].add(jLabels[i]);
            jPanels[i].add(jTextFields[i]);
            jPanels[i].setLayout(new FlowLayout(FlowLayout.CENTER));
        }
        jPanels[jPanels.length - 1].add(jButton1);
        jPanels[jPanels.length - 1].add(jButton2);
//        jPanels[jPanels.length - 1].add(jButton3);

        jPanels[jPanels.length - 1].setLayout(new FlowLayout(FlowLayout.CENTER));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton1) {
            TableInfoTools.changeInfo(this.type, jTextFields);
            this.dispose();
            textTip.dispose();
            MainView.getMainView().informationTable();
            TableInfoTools.printInfo(MainView.getMainView().tableModel);
        }else if (e.getSource() == jButton2){
            this.dispose();
            textTip.dispose();
        }
    }





}
